package cn.edu.zafu.bigdata.yeshuchang.wlbc;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        String hostname = "192.168.16.54";
        int port = 1234; // 服务端端口号

        try {
            Socket socket = new Socket(hostname, port);
            System.out.println("Connected to server.");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            String userInput;
            System.out.println("Enter messages (leave empty to quit):");
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput); // 发送消息到服务端
                String serverResponse = in.readLine(); // 接收服务端的回复
                System.out.println("Server says: " + serverResponse);
                if (userInput.isEmpty()) {
                    break;
                }
            }
            socket.close();
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostname);
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + hostname);
            e.printStackTrace();
        }
    }
}